package MyArrayList;

import java.util.Arrays;

public class MyList implements IList {
    public int[] array;
    public int usedSize;
    public static final int DEFAULT_CAPACITY = 2;
    public MyList() {
        array = new int[DEFAULT_CAPACITY];
    }

    @Override
    public void add(int data) {
        if(isFull()){
            grow();
        }
        array[usedSize] = data;
        usedSize++;//++也可写到数组内
    }
    private void grow(){//2倍扩容方法
        array= Arrays.copyOf(array, 2*array.length);
    }
    private void grow(int growNum){//Num倍扩容方法
        array= Arrays.copyOf(array, growNum*array.length);
    }
    private void checkpos(int pos){//下标合法性检查
        if(pos<0 || pos>usedSize){
            throw new PosOutofBoundsException("pos位置不合法");
        }
    }
    @Override
    public void add(int pos, int data) {
        checkpos(pos);//先检查下标合法性
        if(isFull()){
            grow();
        }
        for(int i=usedSize-1;i>=pos;i--){
            array[i+1] = array[i];//往后移一位
        }
        array[pos]=data;
        usedSize++;
    }


    @Override
    public boolean contains(int toFind) {//判断该元素是否存在在数组中
        for (int i = 0; i < usedSize; i++) {
            if(array[i] == toFind){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexof(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if(array[i]==toFind){
                return i;
            }
        }
        return -1;
    }

    @Override
    public int get(int pos) {
        if(isEmpty()){//先判空再判是否满
            throw new ListEmptyException("获取元素时下标为空！！！");
        }
        checkposget(pos);
        return array[pos];
    }
    private void checkposget(int pos){//下标合法性检查
        if(pos<0 || pos>=usedSize){
            throw new PosOutofBoundsException("pos位置不合法");
        }
    }
    @Override
    public void set(int pos, int value) {
        if(isEmpty()){
            throw new ListEmptyException("下标为不存在！");
        }
        checkposget(pos);
        array[pos] = value;
    }

    @Override
    public void remove(int toRemove) {
        int index = indexof(toRemove);
        if(index==-1){
            System.out.println("没有要删除的数据");
            return;
        }
        for (int i = index; i < usedSize-1; i++) {
            array[i] = array[i+1];
        }
        usedSize--;
    }

    @Override
    public int size() {
        return usedSize;
    }

    @Override
    public void clear() {
        //若并非基本类型需要把数组每个元素指空
//        for (int i = 0; i < usedSize; i++) {
//            array[i] = null;
//        }
        usedSize = 0;
    }

    @Override
    public void display() {
        for (int i = 0; i < usedSize; i++) {
            System.out.print(array[i]+" ");
        }
        System.out.println();
    }

    @Override
    public boolean isFull() {//判断数组是否满了
        return usedSize == array.length;
    }

    @Override
    public boolean isEmpty() {
        return usedSize==0;
    }
}
